package com.haisen.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.haisen.core.utils.InsertBatch;
import com.haisen.core.utils.MyMapper;
import com.haisen.core.utils.PageResultSet;
import com.haisen.core.utils.SpringBeanUtil;
import com.haisen.dataobject.entity.JobVerifyCode;
import com.haisen.dataobject.mapper.base.JobVerifyCodeMapper;
import com.haisen.dataobject.query.JobVerifyCodeQuery;
import com.haisen.service.JobVerifyCodeService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class JobVerifyCodeServiceImpl implements JobVerifyCodeService{


    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    private InsertBatch<JobVerifyCode> batchAdd = null;

    @Autowired
    private JobVerifyCodeMapper jobVerifyCodeMapper;

    @Override
    public void create(JobVerifyCode jobVerifyCode) {
        jobVerifyCodeMapper.insertSelective(jobVerifyCode);
    }

    @Override
    public void update(JobVerifyCode jobVerifyCode) {
        jobVerifyCodeMapper.updateByPrimaryKeySelective(jobVerifyCode);
    }

    @Override
    public void delete(Long[] ids) {
        Weekend<JobVerifyCode> example = Weekend.of(JobVerifyCode.class);
        WeekendCriteria<JobVerifyCode,Object> criteria = example.weekendCriteria();
        criteria.andIn(JobVerifyCode::getId, Arrays.asList(ids));
    }


    @Override
    public void createbatch(List<JobVerifyCode> list) {
        SqlSession sqlSession= sqlSessionFactory.openSession(ExecutorType.BATCH);
         Integer lineNumber = 0;
         Integer insertedNumer = 0;
        int batchCount = 200;// 每批commit的个数
        int batchLastIndex = batchCount;// 每批最后一个的下标
        //设置保存的条数
        lineNumber = list.size();
        try {
            for (int index = 0; index < list.size(); ) {
                if (batchLastIndex >= list.size()) {
                    batchLastIndex = list.size();
                    list.subList(index, batchLastIndex).stream()
                            .map(o -> {
                                return jobVerifyCodeMapper.insertSelective(o);
                            }).collect(Collectors.toList());
                    //设置已保存的数量
                    insertedNumer += batchLastIndex-index;
                    sqlSession.commit();
                    sqlSession.clearCache();
                    break;// 数据插入完毕，退出循环
                } else {
                    list.subList(index, batchLastIndex).stream()
                            .map(o -> {
                                return jobVerifyCodeMapper.insertSelective(o);
                            }).collect(Collectors.toList());
                    //设置已保存的数量
                    insertedNumer += batchLastIndex-index;

                    index = batchLastIndex;// 设置下一批下标
                    sqlSession.commit();
                    sqlSession.clearCache();
                }
                //导入进度
                batchLastIndex = index + (batchCount - 1);
            }
        } finally {
            sqlSession.close();
        }

    }

    @Override
    public PageResultSet<JobVerifyCode> findBypage(JobVerifyCodeQuery jobVerifyCodeQuery) {

        if (!StringUtils.isEmpty(jobVerifyCodeQuery.getOrderBy())){
            PageHelper.orderBy(jobVerifyCodeQuery.getOrderBy());
        }
        PageHelper.offsetPage(jobVerifyCodeQuery.getOffset(),jobVerifyCodeQuery.getLimit());
        Weekend<JobVerifyCode> example = Weekend.of(JobVerifyCode.class);
        WeekendCriteria<JobVerifyCode,Object> criteria = example.weekendCriteria();
        //搜索逻辑判断
        if (!StringUtils.isEmpty(jobVerifyCodeQuery.getUsername())){
            criteria.andEqualTo(JobVerifyCode::getUsername,jobVerifyCodeQuery.getUsername());
        }
        if (!StringUtils.isEmpty(jobVerifyCodeQuery.getTopic())){
            criteria.andEqualTo(JobVerifyCode::getTopic,jobVerifyCodeQuery.getTopic());
        }
       PageResultSet<JobVerifyCode> resultSet = new PageResultSet<>();
        resultSet.setRows(jobVerifyCodeMapper.selectByExample(example));
        resultSet.setTotal(jobVerifyCodeMapper.selectCountByExample(example));
        return resultSet;
    }
}
